const app = getApp()
import http from "../../utils/request.js";
const publicFun = require('../../utils/public');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgList: [],
        imgIds: [],
        content: '',
        feedType: 1,
        radioList: [{
            id: 1,
            label: '紧急Bug',
            checked: true
        }, {
            id: 2,
            label: '建议优化'
        }, {
            id: 3,
            label: '其他'
        }]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    textareaAInput(e) {
        this.setData({
            content: e.detail.value
        })
    },
    radioChange(e) {
        this.data.radioList.forEach(item => {
            item.checked = item.id == e.detail.value
        })
        this.setData({
            radioList: this.data.radioList,
            feedType: e.detail.value
        })
    },
    ChooseImage() {
        let that = this
        wx.chooseMedia({
            count: 4,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            maxDuration: 30,
            camera: 'back',
            success(res) {
                res.tempFiles.forEach(item => {
                    let suffix = item.tempFilePath.substr(item.tempFilePath.lastIndexOf('.') + 1)
                    let params = {
                        imgUrl: item.tempFilePath,
                        userId: app.globalData.userId,
                        type: '问题反馈',
                        suffix: suffix
                    }
                    publicFun.uploadImg(params).then(res => {
                        that.data.imgList.push(res)
                        that.data.imgIds.push(res.id)
                        that.setData({
                            imgList: that.data.imgList,
                            imgIds: that.data.imgIds
                        })
                    })
                })
            }
        })
    },
    ViewImage(e) {
        let imgPreviewList = []
        this.data.imgList.forEach(item => {
            imgPreviewList.push(item.url)
        })
        wx.previewImage({
            urls: imgPreviewList,
            current: e.currentTarget.dataset.url
        });
    },
    // 新增
    addProblemFeedback() {
        let data = {
            userId: app.globalData.userId,
            imgIds: this.data.imgIds.toString(),
            content: this.data.content,
            feedType: this.data.feedType,
        }
        http.Request({
            url: 'newProblemFeedback',
            method: 'POST',
            data: data,
        }).then(re => {
            if (re.code == 200) {
                publicFun.Toast(re.msg)
                this.clear();
            }
        });
    },
    clear() {
        this.data.radioList.forEach(item => {
            if (item.id == 1) {
                item.checked = true
            } else {
                item.checked = false
            }
        })
        this.setData({
            imgList: [],
            imgIds: [],
            content: '',
            feedType: 1,
            radioList: this.data.radioList
        })
    },
    DelImg(e) {
        this.data.imgList.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            imgList: this.data.imgList
        })
        let data = {
            id: e.currentTarget.dataset.id,
            userId: app.globalData.userId
        }
        http.Request({
            url: 'imgDelete',
            method: 'POST',
            data: data,
        }).then(re => {
            if (re.code == 200) {
                // publicFun.Toast(re.msg)
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})